<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        html,
        body,
        #container {
          width: 100%;
          height: 100%;
        }
    </style>
</head>
<body>
<div id="container"></div>
<script type="text/javascript">
    window._AMapSecurityConfig = {
      securityJsCode: "ef8c2f069c73b5e146a1e735dbcfb018",
    };
</script>
<script src="https://webapi.amap.com/loader.js"></script>
<script type="text/javascript">
    var map
    AMapLoader.load({
      key: "ef8c2f069c73b5e146a1e735dbcfb018", //申请好的Web端开发者 Key，调用 load 时必填  在高德开放平台
      version: "2.0", //指定要加载的 JS API 的版本，缺省时默认为 1.4.15
    })
      .then((AMap) => {
         map = new AMap.Map("container");
      })
      .catch((e) => {
        console.error(e); //加载错误提示
      });
</script>
<script>
    // 1
    // window.onmessage = (event) => {

    // }
    // 2
    var port
    window.addEventListener("message", (event) => {
       if(event.data === "mess_port") {
         port = event.ports[0] // 拿到的端口
         onNativeMessage()
         sendMessage({ type: 'alert', value: '老高啊我不行啦' })
        //  alert("我要不行啦！！！！")
       }
    })
    // 发送消息
    function sendMessage (obj) {
        if(port) {
            if(typeof obj !== "string") {
                port.postMessage(JSON.stringify(obj))
            }else {
                port.postMessage(obj)
            }

        }
    }

    function onNativeMessage () {
        if(port) {
            port.onmessage = (event) => {
                map.setZoomAndCenter(16, [event.data.longitude, event.data.latitude])
            //    map.setCenter([event.data.longitude, event.data.latitude]);
            //   map.setZoom(13)
            }
            // port.onmessage = () =>
            // port.addEventListener("message", (event) => {
            // //   alert(event.data)
            //  document.querySelector("#container").innerHTML = ""
            // })
        }

    }

</script>
</body>
</html>